/*
 * @Description: 冒泡 选择 快速
 * @Author: BaldHead
 * @Date: 2022-03-28 17:46:25
 * @LastEditTime: 2022-04-15 20:01:41
 * @LastEditors: Please set LastEditors
 */
/*
 *@Description: 冒泡
 *@FuncAuthor: BaldHead
 *@CreateDate: 2022-03-29 10:58:32
 *@LastEditTime: 
 *@LastEditors: 
 */
function bubble(ary) {
    for (let i = 0; i < ary.length; i++) {
        for (let j = 0; j < ary.length; j++) {
            if (ary[j] > ary[j + 1]) {
                let temp = ary[j];
                ary[j] = ary[j + 1];
                ary[j + 1] = temp;
            }

        }

    }
    return ary;

}
console.log(bubble([12, 8, 15, 12, 1, 24]));
/*
 *@Description: 选择排序
 *@FuncAuthor: BaldHead
 *@CreateDate: 2022-03-29 11:05:48
 *@LastEditTime: 
 *@LastEditors: 
 */
function selection(ary) {
    for (let index = 0; index < ary.length; index++) {
        let min = index;
        for (let j = min; j < ary.length; j++) {
            if (ary[j] < ary[min]) {
                min = j;
            }

        }
        if (min != index) {
            let temp = ary[index];
            ary[index] = ary[min];
            ary[min] = temp;
        }

    }
    return ary;
}
console.log(selection([12, 8, 15, 12, 1, 24]));
/*
 *@Description: 快速排序
 *@FuncAuthor: BaldHead
 *@CreateDate: 2022-03-29 10:14:34
 *@LastEditTime: 
 *@LastEditors: 
 */
function quick(ary) {
    if (ary.length <= 1) {
        return ary;
    }
    let middleIndex = Math.floor(ary.length / 2);
    let middleValue = ary[middleIndex];
    ary.splice(middleIndex, 1);
    let arryLeft = [],
        arrayRight = [];
    for (let index = 0; index < ary.length; index++) {
        let element = ary[index];
        element < middleValue ? arryLeft.push(element) : arrayRight.push(element);

    }
    return quick(arryLeft).concat(middleValue, quick(arrayRight));
}
// function quick(ary) {
//     if (ary.length <= 1) {
//         return ary;
//     }
//     let middleIndex = Math.floor(ary.length / 2);
//     let middleValue = ary.splice(middleIndex, 1)[0];
//     let aryLeft = [],
//         aryRight = [];
//     for (let i = 0; i < ary.length; i++) {
//         let item = ary[i];
//         item < middleValue ? aryLeft.push(item) : aryRight.push(item);
//     }
//     return qucik(aryLeft).concat(middleValue, qucik(aryRight));
// }
let ary = [12, 8, 15, 12, 1, 24];
console.log(quick(ary));